package com.album.activity.adapter;

 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.album.R;  
import com.album.common.Constants;
import com.album.utils.MyLog;
 

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DataAccountExpandablebaseadapter extends BaseExpandableListAdapter {

	private Context context;
	// 准备一级列表中显示的数据
	private Map<Integer,String> groupData = new HashMap<Integer, String>();

	// 准备二级列表中显示的数据
	private List<HashMap<Integer, GridAdapter>> childData = new ArrayList<HashMap<Integer, GridAdapter>>();
	
	private int childDateSize = 0;
	
    private LayoutInflater mChildInflater;  
    private LayoutInflater mGroupInflater; 
	
//	List<List<Map<String, String>>> accountChild = new ArrayList<List<Map<String, String>>>();
	
	public DataAccountExpandablebaseadapter(Context context,
			Map<Integer,String> groupData,
			List<HashMap<Integer, GridAdapter>> childData) {
		this.groupData = groupData;
		this.childData = childData; 
		this.context = context; 
		mGroupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		mChildInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
		childDateSize = this.childData.size();
		Log.d("log", "size:"+childDateSize+"");
	}
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			view = mGroupInflater.inflate(R.layout.member_listview, null);
		}
		//一级列表中显示的标题
		TextView txtFather = (TextView) view.findViewById(R.id.type_name);
		txtFather.setText(groupData.get(groupPosition));
		ImageView image=(ImageView) view.findViewById(R.id.tubiao);
		if(isExpanded)
			image.setBackgroundResource(R.drawable.btn_browser2);
		else 
			image.setBackgroundResource(R.drawable.btn_browser);		
		
		return view;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		GridHolder holder;  
		System.out.println("=============groupPosition===============:"+groupPosition);
		if (convertView == null) {     
			convertView = mChildInflater.inflate(R.layout.gridview_item, null);

//			convertView.setLayoutParams(new LinearLayout.LayoutParams(-1,100));
			holder = new GridHolder();
			holder.gridView = (GridView)convertView.findViewById(R.id.gridView); 
//			holder.gridView.setColumnWidth(Constants.m_screenWidth);
			convertView.setTag(holder);    
		}
		else{
			 holder = (GridHolder) convertView.getTag();
		}
		if(childData.get(groupPosition)!=null){
			MyLog.debug(getClass(), "groupPosition:"+groupPosition+" childPosition:"+childPosition);
			holder.gridView.setAdapter(childData.get(groupPosition).get(groupPosition));
		}  
		return convertView;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}
	@Override
	public Object getGroup(int groupPosition) {
		return groupData.get(groupPosition);
	}
	@Override
	public int getGroupCount() {
		return groupData.size();
	} 
	private class GridHolder {  
		GridView gridView; 
    }  

    public void setNotifyDataSetChanged(){
    	notifyDataSetChanged();
    }
    
    @Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}
    
    @Override
	public boolean hasStableIds() {
		return false;
	}

    @Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	public Object getChild(int groupPosition, int childPosition) {
		return childData.get(groupPosition).get(childPosition);
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		MyLog.info(getClass(), "groupPosition:"+groupPosition+"  childSize:"+childData.get(groupPosition).size());
		return childData.get(groupPosition).size();
	}
}